<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="批次号" prop="batchNum">
        <el-input
          v-model="queryParams.batchNum"
          placeholder="请输入批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物来源类型" prop="goodsType">
        <el-select v-model="queryParams.goodsType" placeholder="请选择">
          <el-option key="0" label="农作物" value="0"></el-option>
          <el-option key="1" label="加工厂" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['source:num:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['source:num:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['source:num:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['source:num:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="numList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="批次编号" align="center" prop="batchId" />
      <el-table-column label="批次号" align="center" prop="batchNum" />
      <el-table-column label="货物名称" align="center" prop="goodsName" />
      <el-table-column label="货物来源类型" align="center" prop="goodsType">
        <template slot-scope="scope">
          <span v-if="scope.row.goodsType==='0'">农作物</span>
          <span v-if="scope.row.goodsType==='1'">加工厂</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['source:num:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['source:num:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改批次号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="批次号" >
          <el-input v-model="form.batchNum" placeholder="请输入客户编码" disabled maxlength="20" style="width: calc(100% - 100px); display: inline-block; margin-right: 3px;" />
          <el-switch
            v-model="value"
            active-color="#13ce66"
            inactive-color="#D5D8DC"
            style="display: inline-block;"
            v-if="form.batchId===null"
            @change="handleSwitchChange">
          </el-switch>
          <span v-if="form.batchId===null">自动生成</span>
        </el-form-item>
        <el-form-item label="货物来源类型" prop="batchNum">
          <el-select v-model="form.goodsType" placeholder="请选择">
            <el-option key="0" label="农作物" value="0"></el-option>
            <el-option key="1" label="加工厂" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绑定货物入口" prop="goodsId">
          <el-button type="text" @click="chooseGoods(form.goodsType)">选择绑定的货物</el-button>
        </el-form-item>
        <el-form-item label="货物编号" prop="remark">
          <el-input v-model="form.goodsId" disabled placeholder="请在上一项选择货物" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-dialog title="货物列表" :visible.sync="dialogTableVisible">
          <el-table
            ref="singleTable"
            :data="goodsData"
            highlight-current-row
            @current-change="handleCurrentChange"
            style="width: 100%">
            <el-table-column property="goodsId" label="货物编号" width="150"></el-table-column>
            <el-table-column property="goodsName" label="货物名称" width="200"></el-table-column>
            <el-table-column property="goodsType" label="货物来源类型" width="200"></el-table-column>
            <el-table-column property="createBy" label="负责人" width="200"></el-table-column>
            <el-table-column property="createTime" label="创建时间"></el-table-column>
          </el-table>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="dialogTableVisible=false">确 定</el-button>
            <el-button @click="goodscancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNum, getNum, delNum, addNum, updateNum, getGoodsList } from "@/api/source/batch/num";

export default {
  name: "Num",
  data() {
    return {
      //
      value:false,
      //货物列表数据
      goodsData:[],
      //内层对话框
      dialogTableVisible:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      numList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        batchNum: null,
        goodsType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSwitchChange(newValue) {
      // newValue 是开关的新状态，true 表示启用，false 表示禁用
      if (newValue) {
        // 如果开关启用时调用的方法
        if (this.form.goodsType==='0'){
          this.form.batchNum='NZW'+this.getProjectNum()+Math.floor(Math.random() * 100000)
        }else {
          this.form.batchNum='JGC'+this.getProjectNum()+Math.floor(Math.random() * 100000)
        }
      }else {
        this.form.batchNum=null;
      }
    },
    // 获取当前日期的方法
    getProjectNum () {
      const projectTime = new Date() // 当前中国标准时间
      const Year = projectTime.getFullYear() // 获取当前年份 支持IE和火狐浏览器.
      const Month = projectTime.getMonth() + 1 // 获取中国区月份
      const Day = projectTime.getDate() // 获取几号
      var CurrentDate = Year.toString().slice(2,4);
      if (Month >= 10) { // 判断月份和几号是否大于10或者小于10
        CurrentDate += Month
      } else {
        CurrentDate += '0' + Month
      }
      if (Day >= 10) {
        CurrentDate += Day
      } else {
        CurrentDate += '0' + Day
      }
      return CurrentDate
    },
    //单选列表
    handleCurrentChange(val) {
      this.form.goodsId = val.goodsId;
      console.log(this.form.goodsId)
    },
    /**选择货物*/
    chooseGoods(goodsType){
      if (goodsType===null){
        this.$message({
          message: '请先选择要绑定的货物来源类型',
          type: 'warning'
        });
      }else {
        getGoodsList(goodsType).then(res=>{
          this.goodsData=res.rows;
          this.dialogTableVisible=true;
        })

      }
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listNum(this.queryParams).then(response => {
        this.numList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //内部对话框取消
    goodscancel(){
      this.dialogTableVisible = false;
      this.form.goodsId = null;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        batchId: null,
        batchNum: null,
        goodsId: null,
        goodsType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isFault: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.batchId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.value=false;
      this.open = true;
      this.title = "添加批次号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const batchId = row.batchId || this.ids
      getNum(batchId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改批次号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.batchId != null) {
            console.log(this.form.remark);
            updateNum(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form.remark);
            addNum(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const batchIds = row.batchId || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + batchIds + '"的数据项？').then(function() {
        return delNum(batchIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/num/export', {
        ...this.queryParams
      }, `num_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
